package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.hmall.common.dto.PageDTO;
import com.hmall.item.constant.MqConstants;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     * 分页查询
     *
     * @param page 当前页数
     * @param size 每页显示条数
     * @return
     */
    @GetMapping("list")
    public PageDTO<Item> getAll(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        log.warn("接收到的页数为:{},条数为:{}", page, size);
        Page<Item> pageList = new Page<>(page, size);

        Page<Item> result = itemService.page(pageList, null);
        return new PageDTO<>(result.getTotal(), result.getRecords());
    }

    /**
     * 根据ID查询商品
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Item getById(@PathVariable("id") Integer id) {
        log.warn("当前查询的ID为:{}", id);
        Item item = itemService.getById(id);
        return item;
    }

    /**
     * 新增商品
     *
     * @param item
     * @return
     */
    @PostMapping
    public boolean save(@RequestBody Item item) {
        log.warn("接收到的数据为:{}", item);
        boolean save = itemService.save(item);
        return save;
    }

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 商品上架、下架
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("status/{id}/{status}")
    public void updateStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status) {
        log.warn("要上架或下架的商品ID为：{}，要修改的状态为状态为：{}", id, status);
        //添加构造器
        LambdaQueryWrapper<Item> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id != null, Item::getId, id);

        Item item = new Item();
        if (status != null) {
            item.setStatus(status);
        }
        itemService.update(item, lqw);

        //同步信息  当status=1返回上架信息  否则返回下架信息
        if (status == 1) {
            rabbitTemplate.convertAndSend(MqConstants.ITEM_DIRECT_EXCHANGE, MqConstants.ITEM_INSERT_KEY, id.toString());
        } else if (status == 2) {
            rabbitTemplate.convertAndSend(MqConstants.ITEM_DIRECT_EXCHANGE, MqConstants.ITEM_DELETE_KEY, id.toString());
        }
    }


    /**
     * 修改商品
     *
     * @param item
     * @return
     */
    @PutMapping
    public boolean updateItem(@RequestBody Item item) {
        log.warn("接收到的修改后的数据为:{}", item);
        //添加构造器
//        LambdaQueryWrapper<Item> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(item != null, Item::getId, item);
        boolean result = itemService.updateById(item);
        return result;
    }


    /**
     * 根据ID删除商品
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public boolean deleteById(@PathVariable("id") Integer id) {
        log.warn("当前删除的商品ID为：{}", id);
        boolean result = itemService.removeById(id);
        return result;
    }

    /**
     * 扣减库存
     * @param id
     * @param num
     */
    @GetMapping("/id/{num}")
    public void updateStock(@PathVariable("id") Long id, @PathVariable("num") Integer num){
        itemService.updateStock(id,num);
    }


}
